package com.bash.carrep.ui.util;

import com.bash.carrep.dao.enties.CreditLevel;
import com.bash.carrep.dao.general.CRUD;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author p.bashizi
 */
public class UserFieldFactory extends PojoFieldFactory {

    public UserFieldFactory() {
    }

    @Override
    public Field createField(Item item, Object propertyId, Component uiContext) {


        Field f = super.createField(item, propertyId, uiContext);

        if ("username".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setCaption("Login");
            tf.setNullRepresentation("");
            tf.setRequiredError("Veuillez donner le login");
            tf.setWidth("15em");

            tf.addValidator(new StringLengthValidator("Le login doit avoir entre 1 et 10 caractères", 1, 10, false));

        }

        if ("password".equals(propertyId)) {
            PasswordField pf = new PasswordField("Mot de passe");
            pf.setRequired(true);
            pf.setWidth("15em");
            pf.setNullRepresentation("");
            pf.setRequiredError("Veuillez donner le mot de passe");
            pf.addValidator(new StringLengthValidator("Le mot de passe doit avoir entre 6 et 10 caractères", 6, 10, false));
            return pf;
        }

        if ("creditLevel".equals(propertyId)) {

            Select sel;
            List<Integer> icls = new ArrayList<Integer>();

            CRUD crud = CRUD.getInstance();
            try {
                crud.start();
                List<CreditLevel> cl = crud.getAll(CreditLevel.class);

                for (CreditLevel c : cl) {
                    icls.add(c.getIdcreditLevel());
                }
            } finally {
                crud.stop();
            }
            sel = new Select("creditLevel", icls);
            sel.setRequiredError("Veuillez donner le credit");
            sel.setWidth("15em");
            // sel.addValidator(new IntegerValidator("Le credit doit être un entier"));

            return sel;
        }

        if ("actif".equals(propertyId)) {
            Select sel;
            Integer[] bols = {0, 1};


            sel = new Select("Actif", Arrays.asList(bols));
            sel.setRequiredError("Veuillez donner l'état de ce login");
            sel.setWidth("15em");

            return sel;

//            Property p = item.getItemProperty(propertyId);
//            CheckBox cb = new CheckBox("Actif");
//             cb.setValue(true);
//            int pval = Integer.valueOf(p.getValue().toString());
//            if (pval == 1) {
//                cb.setValue(true);
//            } else {
//                 cb.setValue(false);
//            }
//            return cb;
        }

        return f;
    }

    @Override
    public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
        return super.createField(container, itemId, propertyId, uiContext);
    }
}
